package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dto.BondManageDTO;
import com.eastfair.venueservice.service.HomeBondManageService;
import com.eastfair.venueservice.vo.BondManageVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 主场保证金 REST API.
 *
 * @author dqq
 * @date 2023 /4/18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/homeBondManage")
@Api(value = "homeBondManage", tags = "主场保证金订单")
@PreAuth(replace = "venueservice:bondManage:")
public class HomeBondManageController {

    @Resource
    private HomeBondManageService homeBondManageService;

    /**
     * 分页查询保证金订单.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/bondManage")
    public R<Page<BondManageVO>> queryBondManage(@RequestBody PageParams<BondManageDTO> pageParams) {
        log.info("queryBondManage - 分页查询保证金订单, pageParams={}", pageParams);
        return R.successDef(homeBondManageService.listBondManageVOs(pageParams));
    }

    /**
     * 展商ID查询保证金订单
     *
     * @param exhibitorIdList the exhibitor id list
     * @return the r
     */
    @PostMapping("/query/exhibitorId")
    public R<List<BondManageVO>> queryByExhibitorIdList(@RequestBody List<Long> exhibitorIdList) {
        log.info("queryBondManage - 展商ID查询保证金订单, pageParams={}", exhibitorIdList);
        try {
            return R.successDef(homeBondManageService.listVOByExhibitorIdList(exhibitorIdList));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 提交保证金订单.
     *
     * @param bondManageDTO the bond manage dto
     * @return the r
     */
    @PostMapping("/submit")
    public R<Boolean> submit(@RequestBody BondManageDTO bondManageDTO) {
        log.info("submit - 提交保证金订单, bondManageDTO={}", bondManageDTO);
        homeBondManageService.submitBondManage(bondManageDTO);
        return R.successDef(true);
    }

    /**
     * 取消保证金订单.
     *
     * @param orderNumber the order number
     * @return the r
     */
    @GetMapping("/cancel/{orderNumber}")
    public R<Boolean> cancel(@PathVariable("orderNumber") String orderNumber) {
        return R.successDef(homeBondManageService.cancelBondManage(orderNumber));
    }
}